package com.atguigu.petstore.exception;

import com.atguigu.petstore.common.execption.NeedLoginPetStoreException;
import com.atguigu.petstore.common.execption.PetstoreException;
import com.atguigu.petstore.common.result.Result;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @author lfy
 * @Description
 * @create 2023-09-22 10:20
 */
@ControllerAdvice //这是集中处理 controller 异常的处理器
public class PetStoreExceptionHandler {




    @ResponseStatus(HttpStatus.FORBIDDEN)  //这种异常，自定义响应状态码
    @ResponseBody
    @ExceptionHandler(NeedLoginPetStoreException.class)
    public Result needLoginPetStoreException(NeedLoginPetStoreException exception){
        //业务抛出的错误码
        Integer code = exception.getCode();
        //业务抛出的异常信息
        String message = exception.getMessage();


        Result<Object> result = new Result<>();

        result.setCode(code);
        result.setMessage(message);

        return result;
    }

    /**
     * 所有业务抛出的 业务异常，都由这个方法处理
     * @return
     */
    @ResponseBody
    @ExceptionHandler(PetstoreException.class)
    public Result petStoreException(PetstoreException exception){
        //业务抛出的错误码
        Integer code = exception.getCode();
        //业务抛出的异常信息
        String message = exception.getMessage();


        Result<Object> result = new Result<>();

        result.setCode(code);
        result.setMessage(message);

        return result;
    }


}
